package com.atguigu.flink.watermark;

import com.atguigu.flink.pojo.WaterSensor;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.TimerService;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

/**
 * Created by Smexy on 2022/11/23
 *
        演示下游从上游的多个并行度接收水印，如何去更新时钟。

                默认取上游中水印最小的来更新时钟，同时把水印广播发送到下游
 */
public class Demo4_MultiParilisimWatermark
{
    public static void main(String[] args) {


        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 3333);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);

        //设置水印周期发送的间隔
        env.getConfig().setAutoWatermarkInterval(2000);

        //初次完，先设置为1
        env.setParallelism(2);

        //水印策略
        WatermarkStrategy<WaterSensor> watermarkStrategy = WatermarkStrategy
            .<WaterSensor>forMonotonousTimestamps()   //目前的场景数据都是有序的
                                                      //如何产生水印
                                                      .withTimestampAssigner((e, r) -> e.getTs());

        env
           .socketTextStream("hadoop103", 8888)
           .map(new MapFunction<String, WaterSensor>()
           {
               @Override
               public WaterSensor map(String value) throws Exception {
                   String[] data = value.split(",");
                   return new WaterSensor(
                       data[0],
                       Long.valueOf(data[1]),
                       Integer.valueOf(data[2])
                   );
               }
           })
           //负责基于水印策略生成水印
           .assignTimestampsAndWatermarks(watermarkStrategy)
           //不加keyBy，默认会执行operator chain，就没有上下游了
           .keyBy(WaterSensor::getId)
           .process(new KeyedProcessFunction<String, WaterSensor, String>()
           {
               @Override
               public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {
                   TimerService timerService = ctx.timerService();

                   long currentWatermark = timerService.currentWatermark();

                   out.collect("数据是:"+value +"----水印(EventTime):"+currentWatermark );

               }
           })
           .print();

        try {
                    env.execute();
                } catch (Exception e) {
                    e.printStackTrace();
                }

    }
}
